package com.javaclimb.filmbuy.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaclimb.filmbuy.common.Consts;
import com.javaclimb.filmbuy.dao.CartInfoDao;
import com.javaclimb.filmbuy.entity.Account;
import com.javaclimb.filmbuy.entity.CartInfo;
import com.javaclimb.filmbuy.entity.GoodsInfo;
import com.javaclimb.filmbuy.exception.CustomException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 收藏service层
* */
@Service
public class CartInfoService {

    @Resource
    private CartInfoDao cartInfoDao;

    @Resource
    private AdminInfoService adminInfoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GoodsInfoService goodsInfoService;

    /**
    * 增加一个收藏
    * */
    public CartInfo add(CartInfo cartInfo){
        //先检查是否已经收藏，如果已经收藏了，就不再收藏了
        Example example = new Example(CartInfo.class);
        example.createCriteria().andEqualTo("userId",cartInfo.getUserId())
                .andEqualTo("level",cartInfo.getLevel())
                .andEqualTo("goodsId",cartInfo.getGoodsId());
        List<CartInfo> cartInfos = cartInfoDao.selectByExample(example);
        if(CollectionUtil.isEmpty(cartInfos)){
            cartInfo.setCreateTime(DateUtil.formatDateTime(new Date()));
            cartInfoDao.insertSelective(cartInfo);
        }
        return cartInfo;
    }

    /**
    * 根据主键ID删除一个收藏
    */
    public void delete(Long id){
        cartInfoDao.deleteByPrimaryKey(id);
    }


    /**
    * 根据主键id查询一个收藏
    * */
    public CartInfo findById(Long id){
        return cartInfoDao.selectByPrimaryKey(id);
    }


    /**
     * 查询所有收藏
     */
    public List<CartInfo> findAll(){
        return cartInfoDao.selectAll();
    }

    /**
     *分页查询
     */
    public PageInfo<CartInfo> findPage(Integer pageNum, Integer pageSize, HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute(Consts.USER);
        if(user == null){
            throw new CustomException("1001","session已失效，请重新登录");
        }
        List<CartInfo> infoList;
        PageHelper.startPage(pageNum,pageSize);
        if(user.getLevel() == 1){
            infoList = cartInfoDao.findAll();
            if(!CollectionUtil.isEmpty(infoList)){
                for(CartInfo cartInfo : infoList){
                    cartInfo.setUserName(adminInfoService.findById(cartInfo.getUserId()).getName());
                }
            }
        }else{
            infoList = cartInfoDao.findCartByUserId(user.getId(),user.getLevel());
            if(!CollectionUtil.isEmpty(infoList)){
                for(CartInfo cartInfo : infoList){
                    cartInfo.setUserName(userInfoService.findById(cartInfo.getUserId()).getName());
                }
            }
        }

        return PageInfo.of(infoList);
    }

    /**
     *前端分页查询，只查自己的
     */
    public PageInfo<GoodsInfo> findFrontPage(Integer pageNum, Integer pageSize, HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute(Consts.USER);
        if(user == null){
            throw new CustomException("1001","session已失效，请重新登录");
        }
        List<GoodsInfo> goodsList = new ArrayList<>();
        PageHelper.startPage(pageNum,pageSize);
        List<CartInfo> cartInfoList  = cartInfoDao.findCartByUserId(user.getId(),user.getLevel());
        if(CollectionUtil.isNotEmpty(cartInfoList)){
            for(CartInfo cartInfo : cartInfoList){
                GoodsInfo goodsInfo = goodsInfoService.findById(cartInfo.getGoodsId());
                if(goodsInfo != null){
                    goodsInfo.setCartInfoId(cartInfo.getId());
                    goodsList.add(goodsInfo);
                }
            }
        }
        return PageInfo.of(goodsList);
    }
}












